package com.shujia.wyh.day12.ketang;

/*

        猫狗案例多态版
 */

class Animal5 {
    private String name;
    private int age;

    public Animal5() {
    }

    public Animal5(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void eat() {
        System.out.println("吃");
    }

    public void sleep() {
        System.out.println("睡觉");
    }
}

class Dog5 extends Animal5 {
    public Dog5() {
    }

    public Dog5(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }

    @Override
    public void sleep() {
        System.out.println("狗躺着睡");
    }

    public void lookDoor() {
        System.out.println("看门");
    }

}

class Cat5 extends Animal5 {
    public Cat5() {
    }

    public Cat5(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

    @Override
    public void sleep() {
        System.out.println("猫蜷着睡");
    }

    public void catchMouse() {
        System.out.println("猫捉老鼠");
    }

}

public class DuoTaiTest1 {
    public static void main(String[] args) {
        //多态创建狗的对象
        Animal5 a = new Dog5("小白", 2);
        a.eat();
        a.sleep();
//        a.lookDoor();
        Dog5 d = (Dog5) a;
        d.lookDoor(); //向下转型使用子类特有的方法

        System.out.println("=================================");
        Animal5 a2 = new Cat5("小花",3);
        a2.eat();
        a2.sleep();
//        a2.catchMouse();
        Cat5 c = (Cat5) a2;
        c.catchMouse();
        System.out.println("==================================");
        a = c;
        a.eat();
        a.sleep();
    }
}
